﻿using Gl_mes.BLL.Mes;
using MesGLApp.DAL.MesDB;
using MesGLApp.DAL.MesDTO;
using Smobiler.Core;
using Smobiler.Core.Controls;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace MesGLApp.View.Search
{
    partial class SearchProdListFrm : Smobiler.Core.Controls.MobileForm
    {
        #region "内部变量"
        private ProdProcessControler _ctrl = new ProdProcessControler();
        private List<SiteInfo> _site_list = new List<SiteInfo>();
        private List<SiteInfo> _parent_list = new List<SiteInfo>();
        private List<StatusInfo> _status_list = new List<StatusInfo>();

        private List<TargetProdProcessDTO> _tar_list = new List<TargetProdProcessDTO>();
        #endregion

        public SearchProdListFrm() : base()
        {
            //This call is required by the SmobilerForm.
            InitializeComponent();
        }

        #region "控件事件"
        /// <summary>
        /// 关闭本页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Press(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 调出大站点的选择项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSite_Press(object sender, EventArgs e)
        {
            this.popSite.ShowDialog();
        }

        /// <summary>
        /// 选择大站点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void popSite_Selected(object sender, EventArgs e)
        {
            this.btnSite.Text = popSite.Selection.Text.Trim();
        }
        #endregion

        #region "内部方法"
        /// <summary>
        /// 查询所有站点，并将站点绑定到popList中
        /// </summary>
        private void InitSitePopList()
        {
            this.popSite.Groups.Clear();
            this._parent_list = this._ctrl.SearchAllParentSiteInfo();
            if (this._parent_list != null && this._parent_list.Count > 0)
            {
                PopListGroup groups = new PopListGroup();
                foreach (var item in this._parent_list)
                {
                    PopListItem popItem = new PopListItem(item.SiteName, item.ID.ToString());
                    groups.Items.Add(popItem);
                }
                this.popSite.Groups.Add(groups);
            }
        }

        /// <summary>
        /// 根据用户选择的大站点找出对应的小站点ID
        /// </summary>
        /// <param name="siteName"></param>
        /// <returns></returns>
        private List<int> GetChileSiteIDForParent(string siteName)
        {
            List<int> resList = new List<int>();
            if (this._parent_list != null)
            {
                var parentQuery = this._parent_list.Where(p => p.SiteName == siteName);
                if (parentQuery != null && parentQuery.Count() > 0 && this._site_list != null && this._site_list.Count > 0)
                {
                    var childQuery = this._site_list.Where(s => s.ParentID == parentQuery.First<SiteInfo>().ID);
                    if (childQuery != null && childQuery.Count() > 0)
                    {
                        resList = childQuery.Select(s => s.ID).Distinct().ToList();
                    }
                }
            }
            return resList;
        }
        #endregion

        /// <summary>
        /// 界面读入时，读取对应的基础资料
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SearchProdListFrm_Load(object sender, EventArgs e)
        {
            //读取大站点信息，做成选择项
            this.InitSitePopList();
            //读取各小站点信息
            this._site_list = this._ctrl.SearchAllSiteInfo();
            //读取各状态信息
            this._status_list = this._ctrl.SearchAllStatus();
        }

        private void btnSearch_Press(object sender, EventArgs e)
        {
            string prodNo = this.txtProdNumber.Text.Trim();
            string mto = this.txtMtoNumber.Text.Trim();
            string mo = this.txtMoNumber.Text.Trim();
            string parentSite = this.btnSite.Text.Trim();
            List<int> siteList = this.GetChileSiteIDForParent(parentSite);

            this._tar_list = this._ctrl.SearchTargetProdForCondition(prodNo, mto, mo, siteList);
            if (this._tar_list != null && this._tar_list.Count > 0)
            {
                DataTable tmpDt = this.ChangeListToDatatable(this._tar_list);
                this.livProd.Rows.Clear();
                this.livProd.DataSource = tmpDt;
                this.livProd.DataBind();
            }
            else
            {
                MessageBox.Show("没有找到相关的工单资料！", "查询结果");
            }
        }


        private DataTable ChangeListToDatatable(List<TargetProdProcessDTO> tarList)
        {
            DataTable resDt = new DataTable();
            resDt.Columns.Add("id", typeof(int));
            resDt.Columns.Add("prod_number", typeof(string));
            resDt.Columns.Add("mto_number", typeof(string));
            resDt.Columns.Add("mo_number", typeof(string));
            resDt.Columns.Add("prod_qty", typeof(decimal));
            resDt.Columns.Add("prod_status_id", typeof(int));
            resDt.Columns.Add("prod_status_name", typeof(string));

            if (this._tar_list != null && this._tar_list.Count > 0)
            {
                foreach (var item in this._tar_list.OrderBy(t => t.ProdNumber))
                {
                    DataRow addRow = resDt.NewRow();
                    addRow["id"] = item.ID;
                    addRow["prod_number"] = item.ProdNumber;
                    addRow["mto_number"] = item.MtoNumber;
                    addRow["mo_number"] = item.MoNumber;
                    addRow["prod_qty"] = Math.Round(item.ProdQty ?? 0,2);
                    addRow["prod_status_id"] = item.ProdStatusID;
                    addRow["prod_status_name"] = "";
                    if (this._status_list != null && this._status_list.Count > 0)
                    {
                        var query = this._status_list.Where(s => s.ID == item.ProdStatusID);
                        if (query != null && query.Count() > 0)
                        {
                            addRow["prod_status_name"] = query.First().StatusValue;
                        }
                    }
                    resDt.Rows.Add(addRow);
                }
            }

            return resDt;
        }
    }
}